package com.Model.DengLuModel;

import com.Model.GetSqlSession;
import com.Model.Pojo.Users;
import com.sql.DAO;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class PanDuanZhangHao {
	
	private String name;
	private String pass;
	private String user;
	
	public void setPass(String pass) {
		this.pass = pass;
	}
	public String getPass() {
		return pass;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getUser() {
		return user;
	}
	
	public PanDuanZhangHao(String name, String pass, String user){
		this.name = name;
		this.pass = pass;
		this.user = user;
		if(user.equals("adm")){ //管理员登录还有问题，暂时这样处理
			this.user = "users";
		}else{
			this.user = "users";
		}
	}
	
	public String panDuan(){ //判断账号密码是否能够登录
		String flag;
		if(name != null && pass != null){ //第三次确定信息非空
			pass = pass + "1";
			DAO dao = new DAO(); //初始化dao
			if(dao.login(pingsql("name"))){//判断账号密码/手机号密码/邮箱密码的组合哪个正确，正确即赋值相应列名
				flag = "name"; 
			}else if(dao.login(pingsql("shoujihao"))){
				flag = "shoujihao";
			}else if(dao.login(pingsql("youxiang"))){
				flag = "youxiang";
			}else{
				flag = "false";
			}
		}else{
			flag = "false";
		}
		return flag;
	}
	
	public String pingsql(String type){ //拼接sql语句
		String sql;
		sql = "select * from " + user + " where " + type + " = '" + name + "' and pass = '" + pass + "'";
		return sql;
	}

	public Users getUsersByNameOrShoujihaoOrYouxiang(String flag, String name, String pass) throws IOException {
		GetSqlSession getSqlSession = new GetSqlSession();//初始化获取sqlSession的对象
		SqlSession sqlSession = getSqlSession.getSqlSession();//获取sqlSession

		Map<String,Object> params = new HashMap<>();//将查询语句需要的参数放入map
		params.put("flag", flag);
		params.put("name", name);
		params.put("pass", pass+"1");
		Users users = sqlSession.selectOne("getUsersByNameOrShoujihaoOrYouxiang",params);//获取user的详细信息

		sqlSession.commit();
		sqlSession.close();//关闭连接
		return users;
	}
}
